package cn.wolfcode.gitflow.web.controller;

import cn.wolfcode.web.annotation.RequiredPermission;
import cn.wolfcode.web.domain.Department;
import cn.wolfcode.web.domain.Employee;
import cn.wolfcode.web.domain.Role;
import cn.wolfcode.web.jsonResult.JsonResult;
import cn.wolfcode.web.query.EmployeeQueryObject;
import cn.wolfcode.web.service.IDepartmentService;
import cn.wolfcode.web.service.IEmployeeService;
import cn.wolfcode.web.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class UserController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;



    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {

        try {
            employeeService.batchDelete(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }

    }

    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String name) {
        //        后台检查用户名是否存在
        Employee employee = employeeService.selectByName(name);

//        插件要求返回结果需要为键值对形式 key为valid  ，value为boolean类型
        Map<String, Boolean> map = new HashMap();
        map.put("valid", employee == null);////employee有值返回true,反之
        return map;
    }

    @RequestMapping("/input")
    public String input(Long id, Model model) {
        System.out.println("11");
        if (id != null) {
            Employee employee = employeeService.get(id);
            List<Role> roles = roleService.selectById(employee.getId());
            model.addAttribute("roles", roles);
            List<Department> departments = departmentService.listAll();
            model.addAttribute("departments", departments);
            model.addAttribute("employee", employee);


        }
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles", roles);
        return "employee/input";
    }

    //    统一异常处理:配置了处理控制器异常处理类,并且交给容器管理后,它帮我们在底层捕获异常了try-cath异常了.
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(value = "xx删除", expression = "xxx:delete")
    public String saveOrUpdate(Employee employee, Long[] ids) {

        if (employee.getId() != null) {
            employeeService.update(employee);//如果调用方法不成功,程序也不会终止,因为
//            统一异常处理:配置了处理控制器异常处理类,并且交给容器管理后,它帮我们在底层捕获异常了try-cath异常了.
        } else {
            employeeService.save(employee);
        }
        if (ids != null && ids.length > 0) {
            for (Long roleId : ids) {
                employeeService.insertRelation(employee.getId(), roleId);
            }
        }
        return "redirect:/employee/list.do";//employee/saveOrUpdate.do    "/department/saveOrUpdate.do"
    }
}
